package cn.tedu.suppermemory.controller;

import cn.tedu.suppermemory.pojo.dto.RoleAddNewDTO;
import cn.tedu.suppermemory.pojo.dto.RoleUpdateDTO;
import cn.tedu.suppermemory.pojo.dto.TeacherAddDTO;
import cn.tedu.suppermemory.pojo.dto.TeacherUpdateDTO;
import cn.tedu.suppermemory.pojo.vo.RoleListItemVO;
import cn.tedu.suppermemory.pojo.vo.RoleStandardVO;
import cn.tedu.suppermemory.pojo.vo.TeacherListItemVO;
import cn.tedu.suppermemory.pojo.vo.TeacherStandardVO;
import cn.tedu.suppermemory.service.IRoleService;
import cn.tedu.suppermemory.service.ITeacherService;
import cn.tedu.suppermemory.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 处理角色相关请求的控制器类
 *
 * @author java@xuchuang
 * @version 0.0.1
 */
@Api(tags = "002. 角色管理模块")
@Slf4j
@RestController
@RequestMapping("/roles")
@Validated
public class RoleController {

    @Autowired
    private IRoleService roleService;

    public RoleController() {
        log.debug("创建控制器类对象：RoleController");
    }

    @PostMapping("/add-new")
    @ApiOperation("添加角色")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid RoleAddNewDTO roleAddDTO) {
        log.debug("开始处理【添加相册】的请求，参数：{}", roleAddDTO);
        roleService.addNew(roleAddDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除角色")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "角色ID", required = true,  dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 1000, message = "删除角色失败，角色ID非法！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除角色】的请求，参数：{}", id);
        roleService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改角色详情")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable Long id, RoleUpdateDTO roleUpdateDTO) {
        log.debug("开始处理【修改角色详情】的请求，参数：{}", roleUpdateDTO);
        roleService.updateInfoById(id, roleUpdateDTO);
        return JsonResult.ok();
    }


    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询角色详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "角色ID", required = true,  dataType = "long")
    public JsonResult<RoleStandardVO> getStandardById(@Range(min = 1, max = 1000, message = "查询角色详情失败，角色ID非法！")
                                                       @PathVariable Long id) {
        log.debug("开始处理【根据id查询角色详情】的请求，参数：{}", id);
        RoleStandardVO roleStandardVO = roleService.getStandardById(id);
        return JsonResult.ok(roleStandardVO);
    }


    @GetMapping("")
    @ApiOperation("查询角色列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<RoleListItemVO>> list() {
        log.debug("开始处理【查询角色列表】的业务，无参数");
        List<RoleListItemVO> list = roleService.list();
        return JsonResult.ok(list);
    }


}
